package net.mysoft.budgetsoft.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.dao.DictionaryTypeDao;
import net.mysoft.budgetsoft.system.domain.TDictionaryType;
import net.mysoft.budgetsoft.system.pagemodel.DictionaryType;
import net.mysoft.budgetsoft.system.service.DictionaryTypeService;
import net.mysoft.framework.model.DataGrid;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**   
 *  
 * @author dagongPGP v1.0 
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a> 
 *  
 * @version $Revision: 1.00  $Date: 2013-02-06 11:01:30 
 */
@Service("dictionaryTypeService")
public class DictionaryTypeServiceImpl implements DictionaryTypeService{
	@Resource
	private DictionaryTypeDao dictionaryTypeDao;
	
	public DictionaryTypeDao getDictionaryTypeDao(){
		return this.dictionaryTypeDao;
	}
	
	public void setDictionaryTypeDao(DictionaryTypeDao dictionaryTypeDao){
		this.dictionaryTypeDao = dictionaryTypeDao;
	}
	
	@Override
	public void add(DictionaryType dictionaryType){
		TDictionaryType t = new TDictionaryType();
		BeanUtils.copyProperties(dictionaryType, t);
		t.setTypeID(UUID.randomUUID().toString());
		dictionaryTypeDao.save(t);
	}
	
	@Override
	public void edit(DictionaryType dictionaryType){
		TDictionaryType t = new TDictionaryType();
		BeanUtils.copyProperties(dictionaryType, t);
		dictionaryTypeDao.update(t);
	}
	
	@Override
	public void delete(DictionaryType dictionaryType) {
		for (String id : dictionaryType.getIds().split(",")) {
			Object t =  dictionaryTypeDao.get(TDictionaryType.class, id);
			if (t != null) {
				 dictionaryTypeDao.delete(t);
			}
		}
	}
	
	@Override
	public DataGrid datagrid(DictionaryType dictionaryType){
		DataGrid dg = new DataGrid();
		String hql = "from TDictionaryType t ";
		List params = new ArrayList();
		hql = addWhere(dictionaryType, hql, params);
		String totalHql = "select count(*) " + hql;
		hql = addOrder(dictionaryType, hql);
		List<TDictionaryType> toList = dictionaryTypeDao.find(hql, params, dictionaryType.getPage(), dictionaryType.getRows());
		List<DictionaryType> oList = new ArrayList<DictionaryType>();
		oList = changeModel(toList);
		dg.setTotal(dictionaryTypeDao.count(totalHql, params));
		dg.setRows(oList);
		return dg;
	}
	
	@Override
	public List<DictionaryType> find(DictionaryType dictionaryType) {
		List<Object> params = new ArrayList();
		String hql = addOrder(dictionaryType,addWhere(dictionaryType,"from TDictionaryType t where 1=1 ",params));
		List<TDictionaryType> toList = dictionaryTypeDao.find(hql,params);
		List<DictionaryType> oList = new ArrayList<DictionaryType>();
		oList = changeModel(toList);
		return oList;
	}
	
	private String addWhere(DictionaryType dictionaryType, String hql, List<Object> params) {
		if(dictionaryType.getTypeID()!=null&&!dictionaryType.getTypeID().equals("")){
			hql += "and t.typeID = ? ";
			params.add(dictionaryType.getTypeID());
		}
		return hql;
	}
	
	private String addOrder(DictionaryType dictionaryType, String hql) {
		if (dictionaryType.getSort() != null) {
			hql += " order by " + dictionaryType.getSort() + " " + dictionaryType.getOrder();
		}
		return hql;
	}
	
	public List<DictionaryType> changeModel(List<TDictionaryType> tDictionaryType ){
		List<DictionaryType> list = new ArrayList<DictionaryType>();
		for(TDictionaryType to:tDictionaryType){
			DictionaryType o = new DictionaryType();
			BeanUtils.copyProperties(to, o);
			list.add(o);
		}
		return list;
	}


}
